﻿using Dapper;
using YinfangWeb.IDAL;
using YinfangWeb.Model;
using YinfangWeb.Common;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace YinfangWeb.DAL
{
	/// <summary>
	/// ProductParam的数据操作类
	/// </summary>
	public partial class ProductParamDAO : IProductParamDAO
	{
        #region 添加单条记录
        /// <summary>
		/// 添加单条记录
		/// </summary>
		/// <param name="productParam">实体</param>
        /// <param name="trans">事务</param>
		/// <returns>影响行数</returns>
        public int Insert(ProductParam productParam, IDbTransaction trans = null)
        {
            var sql = "INSERT INTO ProductParam(ProductDataID, ParamDefault, OrderNum, ParamName1, ParamValue1, ParamName2, ParamValue2, ParamName3, ParamValue3, PriceMarket, PriceSell, PriceNet, PriceTax, PricePost, PriceCost, CoinDeduct, Stock, SellSum, DefaultProductFileID) VALUES (@ProductDataID, @ParamDefault, @OrderNum, @ParamName1, @ParamValue1, @ParamName2, @ParamValue2, @ParamName3, @ParamValue3, @PriceMarket, @PriceSell, @PriceNet, @PriceTax, @PricePost, @PriceCost, @CoinDeduct, @Stock, @SellSum, @DefaultProductFileID);SELECT CAST(SCOPE_IDENTITY() AS int)";

            if (trans != null)
            {
                var conn = trans.Connection;
                var productParamID = conn.Query<int>(sql, productParam, trans).Single();
                productParam.ProductParamID = productParamID;
                var result = 1;
                return result;
            }
            using (var conn = ConnectionFactory.CreateConnection())
            {
                conn.Open();
                var productParamID = conn.Query<int>(sql, productParam).Single();
                productParam.ProductParamID = productParamID;
                var result = 1;
                conn.Close();
                return result;
            }
        }
        #endregion
        #region 添加多条记录
        /// <summary>
		/// 添加多条记录
		/// </summary>
		/// <param name="list">列表</param>
        /// <param name="trans">事务</param>
		/// <returns>影响行数</returns>
        public int Insert(List<ProductParam> list, IDbTransaction trans = null)
        {
            var sql = "INSERT INTO ProductParam(ProductDataID, ParamDefault, OrderNum, ParamName1, ParamValue1, ParamName2, ParamValue2, ParamName3, ParamValue3, PriceMarket, PriceSell, PriceNet, PriceTax, PricePost, PriceCost, CoinDeduct, Stock, SellSum, DefaultProductFileID) VALUES (@ProductDataID, @ParamDefault, @OrderNum, @ParamName1, @ParamValue1, @ParamName2, @ParamValue2, @ParamName3, @ParamValue3, @PriceMarket, @PriceSell, @PriceNet, @PriceTax, @PricePost, @PriceCost, @CoinDeduct, @Stock, @SellSum, @DefaultProductFileID)";
            var result = 0;
            if (trans != null)
            {
                var conn = trans.Connection;
                result = conn.Execute(sql, list, trans);
                return result;
            }
            using (var conn = ConnectionFactory.CreateConnection())
            {
                conn.Open();
                result = conn.Execute(sql, list);
                conn.Close();
                return result;
            }
        }
        #endregion
        #region 编辑单条记录
        /// <summary>
		/// 编辑单条记录
		/// </summary>
		/// <param name="productParam">实体</param>
        /// <param name="trans">事务</param>
		/// <returns>影响行数</returns>
        public int Update(ProductParam productParam, IDbTransaction trans = null)
        {
            var sql = "UPDATE ProductParam SET ProductDataID = @ProductDataID, ParamDefault = @ParamDefault, OrderNum = @OrderNum, ParamName1 = @ParamName1, ParamValue1 = @ParamValue1, ParamName2 = @ParamName2, ParamValue2 = @ParamValue2, ParamName3 = @ParamName3, ParamValue3 = @ParamValue3, PriceMarket = @PriceMarket, PriceSell = @PriceSell, PriceNet = @PriceNet, PriceTax = @PriceTax, PricePost = @PricePost, PriceCost = @PriceCost, CoinDeduct = @CoinDeduct, Stock = @Stock, SellSum = @SellSum, DefaultProductFileID = @DefaultProductFileID WHERE ProductParamID = @ProductParamID";
            var result = 0;
            if (trans != null)
            {
                var conn = trans.Connection;
                result = conn.Execute(sql, productParam, trans);
                return result;
            }
            using (var conn = ConnectionFactory.CreateConnection())
            {
                conn.Open();
                result = conn.Execute(sql, productParam);
                conn.Close();
                return result;
            }
        }
        #endregion
        #region 编辑多条记录
        /// <summary>
		/// 编辑多条记录
		/// </summary>
		/// <param name="list">实体列表</param>
        /// <param name="trans">事务</param>
		/// <returns>影响行数</returns>
        public int Update(List<ProductParam> list, IDbTransaction trans = null)
        {
            var sql = "UPDATE ProductParam SET ProductDataID = @ProductDataID, ParamDefault = @ParamDefault, OrderNum = @OrderNum, ParamName1 = @ParamName1, ParamValue1 = @ParamValue1, ParamName2 = @ParamName2, ParamValue2 = @ParamValue2, ParamName3 = @ParamName3, ParamValue3 = @ParamValue3, PriceMarket = @PriceMarket, PriceSell = @PriceSell, PriceNet = @PriceNet, PriceTax = @PriceTax, PricePost = @PricePost, PriceCost = @PriceCost, CoinDeduct = @CoinDeduct, Stock = @Stock, SellSum = @SellSum, DefaultProductFileID = @DefaultProductFileID WHERE ProductParamID = @ProductParamID";
            var result = 0;
            if (trans != null)
            {
                var conn = trans.Connection;
                result = conn.Execute(sql, list, trans);
                return result;
            }
            using (var conn = ConnectionFactory.CreateConnection())
            {
                conn.Open();
                result = conn.Execute(sql, list);
                conn.Close();
                return result;
            }
        }
        #endregion
        #region 删除单条记录
        /// <summary>
		/// 删除单条记录
		/// </summary>
		/// <param name="productParamID">主键</param>
        /// <param name="trans">事务</param>
		/// <returns>影响行数</returns>
        public int Delete(int productParamID, IDbTransaction trans = null)
        {
            var sql = "DELETE ProductParam WHERE ProductParamID = @ProductParamID";
            var result = 0;
            if (trans != null)
            {
                var conn = trans.Connection;
                result = conn.Execute(sql, new { ProductParamID = productParamID }, trans);
                return result;
            }
            using (var conn = ConnectionFactory.CreateConnection())
            {
                conn.Open();
                result = conn.Execute(sql, new { ProductParamID = productParamID });
                conn.Close();
                return result;
            }
        }
        #endregion
        #region 删除单条记录
        /// <summary>
		/// 删除单条记录
		/// </summary>
		/// <param name="productParam">实体</param>
        /// <param name="trans">事务</param>
		/// <returns>影响行数</returns>
        public int Delete(ProductParam productParam, IDbTransaction trans = null)
        {
            var sql = "DELETE ProductParam WHERE ProductParamID = @ProductParamID";
            var result = 0;
            if (trans != null)
            {
                var conn = trans.Connection;
                result = conn.Execute(sql, productParam, trans);
                return result;
            }
            using (var conn = ConnectionFactory.CreateConnection())
            {
                conn.Open();
                result = conn.Execute(sql, productParam);
                conn.Close();
                return result;
            }
        }
        #endregion
        #region 获取单条记录
        /// <summary>
		/// 获取单条记录
		/// </summary>
		/// <param name="productParamID">主键</param>
		/// <returns>实体</returns>
        public ProductParam GetInfo(int productParamID)
        {
            var sql = "SELECT * FROM ProductParam WHERE ProductParamID = @ProductParamID";
            using (var conn = ConnectionFactory.CreateConnection())
            {
                var model = conn.QueryFirstOrDefault<ProductParam>(sql, new { ProductParamID = productParamID });
                return model;
            }
        }
        #endregion
        #region 获取全部记录列表
        /// <summary>
		/// 获取全部记录列表
		/// </summary>
		/// <returns>实体列表</returns>
        public List<ProductParam> GetList()
        {
            var sql = "SELECT * FROM ProductParam";
            using (var conn = ConnectionFactory.CreateConnection())
            {
                var q = conn.Query<ProductParam>(sql);
                return q.ToList();
            }
        }
        #endregion
        #region 分页列表
        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="orderBy">排序规则</param>
        /// <param name="whereClause">条件语句</param>
        /// <returns>分页列表</returns>
        public PagedList<ProductParam> GetList(int pageIndex, int pageSize, string whereClause = null, string orderBy = null)
        {
            pageIndex--;
            if (!string.IsNullOrEmpty(whereClause))
            {
                whereClause = " WHERE " + whereClause;
            }
            if (string.IsNullOrEmpty(orderBy))
            {
                orderBy = "ProductParamID DESC";
            }
            var startItem = pageIndex * pageSize + 1;
            var endItem = (pageIndex + 1) * pageSize;

            var sql = $@"SELECT COUNT(ProductParamID) FROM ProductParam {whereClause} 
SELECT *
FROM(SELECT ROW_NUMBER() OVER(ORDER BY {orderBy}) AS RowNum, *
          FROM ProductParam
          {whereClause}
        ) AS result
WHERE RowNum >= {startItem}
    AND RowNum <= {endItem}
ORDER BY RowNum";
            using (var conn = ConnectionFactory.CreateConnection())
            {
                conn.Open();
                int count = 0;
                var list = new List<ProductParam>();
                using (var q = conn.QueryMultiple(sql))
                {
                    count = q.Read<int>().Single();
                    list = q.Read<ProductParam>().ToList();
                }
                conn.Close();

                var pagedList = new PagedList<ProductParam>(list, pageIndex, pageSize, count);
                return pagedList;
            }
        }
        #endregion
        #region 数据操作类
		/// <summary>
		/// ProductParam的数据操作类
		/// </summary>
		public ProductParamDAO()
		{
		}
        #endregion
	}
}

